package com.kclm.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @Author : lvhx
 * @Date: 2025/10/29 9:42
 * @Description:
 */
public class SqlSessionUtil {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try{
             InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }catch (Exception e){
            e.printStackTrace();
            throw   new RuntimeException("初始化mybatis异常");
        }
    }
    //定义一个静态方法
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}
